package com.oscar.ehome.view.pageview.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.bean.pagebean.ApplationsBean;
import com.oscar.ehome.bean.pagebean.PageAppBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/6/28
 * 添加view  adapter
 */

public class AddGridViewAdapter extends BaseAdapter {

    private List<String> specailList = new ArrayList<>();
    private LayoutInflater mInflater;
    private Context mContext = null;
    public OnClickslister onClickListener;
    int size = 0;
    //是否展示删减图标
    int update = 0;

    public AddGridViewAdapter(Context context, List<String> specailList) {
        this.mContext = context;
        this.specailList = specailList;
        mInflater = LayoutInflater.from(context);
    }

    public void setList(List<String> list) {
        this.specailList = list;
        notifyDataSetChanged();
    }

    public void setUpdate(int update) {
        this.update = update;
        notifyDataSetChanged();
    }

    /**
     * 设置回调接口
     *
     * @param onClickListener
     */
    public void setOnClicksListener(OnClickslister onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickslister {
        public void OnClick(int postion);
    }

    @Override
    public int getCount() {
        if (specailList == null) {
            size = 0;
        } else {
            size = specailList.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        return specailList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_addgridview, null);
            holder = new ViewHolder();
            holder.add_text = (TextView) convertView.findViewById(R.id.add_text);
            holder.add_gridlayout = (LinearLayout) convertView.findViewById(R.id.add_gridlayout);
            holder.add_img = (ImageView) convertView.findViewById(R.id.add_img);
            holder.add_gridimg = (ImageView) convertView.findViewById(R.id.add_gridimg);
            holder.add_gridlayouts = (RelativeLayout) convertView.findViewById(R.id.add_gridlayouts);
            convertView.setTag(holder); //
        } else {
            holder = (ViewHolder) convertView.getTag(); //
        }
        if (update == 1) {
            Resources res = mContext.getResources();
            Drawable draw = res.getDrawable(R.drawable.bianjibiankuang);
            //设置linearLayout的背景图，将Drawable对象放进去
            holder.add_gridlayouts.setBackground(draw);

            holder.add_gridimg.setVisibility(View.VISIBLE);



        } else {
            holder.add_gridlayouts.setBackgroundColor(Color.parseColor("#ffffff"));
            holder.add_gridimg.setVisibility(View.GONE);
        }

        holder.add_gridimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.OnClick(position);
            }
        });

        if (specailList.get(position) != null && !"".equals(specailList.get(position))) {
            String stats = specailList.get(position);

            switch (stats) {
                case "8"://应用中心
                    holder.add_text.setText(R.string.yyzx);
                    holder.add_img.setImageResource(R.mipmap.yyzx);
                    break;
                case "7"://考勤签到
                    holder.add_text.setText(R.string.kqqd);
                    holder.add_img.setImageResource(R.mipmap.indexqian);
                    break;
                case "6"://统计
                    holder.add_text.setText(R.string.tj);
                    holder.add_img.setImageResource(R.mipmap.indextongji);
                    break;
                case "9"://准会员
                    holder.add_text.setText(R.string.zhy);
                    holder.add_img.setImageResource(R.mipmap.huiyuan);
                    break;
                case "4"://学习管理
                    holder.add_text.setText(R.string.study);
                    holder.add_img.setImageResource(R.mipmap.xuexi);
                    break;
                case "3"://活动管理
                    holder.add_text.setText(R.string.hd);
                    holder.add_img.setImageResource(R.mipmap.huodong);
                    break;
                case "11"://寿险通
                    holder.add_text.setText(R.string.sxt);
                    holder.add_img.setImageResource(R.mipmap.sxt);
                    break;
                case "10"://掌中宝
                    holder.add_text.setText(R.string.zzb);
                    holder.add_img.setImageResource(R.mipmap.zzb);
                    break;
                case "12"://保险档案
                    holder.add_img.setImageResource(R.mipmap.dangan1);
                    holder.add_text.setText(R.string.insurance_text);
                    break;
                case "13"://工作日志
                    holder.add_img.setImageResource(R.mipmap.jilu);
                    holder.add_text.setText(R.string.log_text);
                    break;
                case "14"://工作日志
                    holder.add_img.setImageResource(R.mipmap.zhagnzhishi);
                    holder.add_text.setText(R.string.know_text);
                    break;
                case "15"://获客
                    holder.add_img.setImageResource(R.mipmap.huoke);
                    holder.add_text.setText(R.string.huoke);
                    break;
                case "16"://考勤签到
                    holder.add_text.setText(R.string.kqqd);
                    holder.add_img.setImageResource(R.mipmap.indexqian);
                    break;
                case "17"://督导
                    holder.add_text.setText(R.string.dudao);
                    holder.add_img.setImageResource(R.mipmap.dudaowang);
                    break;
                case "18"://nimingjubao
                    holder.add_img.setImageResource(R.mipmap.nimingjubao);
                    holder.add_text.setText(R.string.nimingjubao);
                    break;
                case "19"://nimingjubao
                    holder.add_img.setImageResource(R.mipmap.mingpian);
                    holder.add_text.setText(R.string.yaoqinghan);
                    break;
                case "20"://nimingjubao
                    holder.add_img.setImageResource(R.mipmap.youxi);
                    holder.add_text.setText(R.string.youxi);
                    break;
                case "21":
                    holder.add_img.setImageResource(R.mipmap.tuoguan);
                    holder.add_text.setText(R.string.baodantuoguan);
                    break;
                case "22":
                    holder.add_img.setImageResource(R.mipmap.yejichaxun);
                    holder.add_text.setText(R.string.yejichaxun);
                    break;
            }
        }

        return convertView;
    }

    class ViewHolder {
        TextView add_text;
        LinearLayout add_gridlayout;
        ImageView add_img;
        ImageView add_gridimg;//添加按钮
        RelativeLayout add_gridlayouts;
    }
}
